package com.kexuexiong.demo.bearBaseService.controller.file;


import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.kexuexiong.demo.common.dto.Result;
import com.kexuexiong.demo.common.feignProvider.baseService.file.FileFeignClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileOutputStream;
import java.io.IOException;

@Api(tags = {"首页"})
@RestController
@RequestMapping("/file")
@Slf4j
public class FileController implements FileFeignClient {

    @NacosValue(value = "${file.temp.dir:d:/}", autoRefreshed = true)
    private String fileTempDir;


    @RequestMapping(value = "/uploadFile", method = {RequestMethod.POST},consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @ApiOperation(value = "文件上传", notes = "文件上传")
    @Override
    public Result uploadFile(@Parameter(name = "file", required = true) @RequestPart("file") MultipartFile file) throws IOException {
        String filePath = fileTempDir + "/"+file.getOriginalFilename();
        log.info("文件上传，文件名：{},输出文件名：{}", file.getOriginalFilename(), filePath);
        FileCopyUtils.copy(file.getInputStream(), new FileOutputStream(filePath));
        return Result.success();
    }
}
